package com.czk.netty.netty.tcp.protocaltcp.practice.client;

import com.czk.netty.netty.tcp.protocaltcp.practice.protocol.TransferProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/6/8 11:24
 */
public class MessageEncoder extends MessageToByteEncoder<TransferProtocol> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, TransferProtocol transferProtocol, ByteBuf byteBuf) throws Exception {
        System.out.println("编码器调用了");
        // 拿到数据
        byte[] content = transferProtocol.getContent();
        int length = transferProtocol.getLength();
        byteBuf.writeInt(length);
        byteBuf.writeBytes(content);
    }
}
